const downloadGitRepo = require('download-git-repo')
const ora = require('ora')
const util = require('util')
const chalk = require('chalk')

const REPO_TEMP_MAP = {
  h5: 'direct:https://gitee.com/fisher810/vite-vue-ts-h5.git',
  pc: 'direct:https://gitee.com/fisher810/vite-vue-ts-pc.git',
  nestjs: 'direct:https://gitee.com/fisher810/nestjs-be.git',
}
const download = util.promisify(downloadGitRepo)
module.exports = async (templateName, name, option) => {
  const gitUrl = REPO_TEMP_MAP[templateName]
  if (gitUrl) {
    const spinner = ora('正在下载...').start()
    try {
      const result = await download(gitUrl, name, option)
      spinner.succeed('下载完成')
      // 4）模板使用提示
      console.log(`\r\nSuccessfully created project ${chalk.cyan(name)}`)
      console.log(`\r\n  cd ${chalk.cyan(name)}`)
      console.log(`\r\n  pnpm install`)
      console.log('  pnpm run dev\r\n')
      return result
    } catch (error) {
      // 状态为修改为失败
      spinner.fail('下载失败')
    } 
  }
}